package com.westcom.vulb.activity;

import com.westcom.vulb.bean.Session;
import com.westcom.vulb.logic.account;
import com.westcom.vulb.protocal.http.Response;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class RegisterActivity extends BasicActivity
{
	EditText mEtEmail = null;
	EditText mEtEmailConfirm = null;
	EditText mEtPwd = null;
	EditText mEtPwdConfirm = null;
	EditText mEtNickname= null;
	CheckBox mCbAgree = null;
	TextView mTvDeclartion = null;
	
	TextView mTvBack = null;
	TextView mTvDone = null;
	
	String mEmail = null;
	String mEmailConfirm = null;
	String mPwd = null;
	String mPwdConfirm = null;
	String mNickname = null;
	
	ProgressDialog mLoadingDialog = null;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity_layout);
        init();
    }
    private void init()
    {
    	mEtEmail = (EditText)this.findViewById(R.id.et_reg_email);
        mEtEmailConfirm = (EditText)this.findViewById(R.id.et_reg_email_confirm);
        mEtPwd = (EditText)this.findViewById(R.id.et_reg_pwd);
        mEtPwdConfirm = (EditText)this.findViewById(R.id.et_reg_pwd_confirm);
        mEtNickname = (EditText)this.findViewById(R.id.et_reg_nickname);
        mCbAgree = (CheckBox)this.findViewById(R.id.cb_reg_agreee);
        mTvDeclartion = (TextView)this.findViewById(R.id.tv_reg_declartion);
        mTvDone = (TextView)this.findViewById(R.id.reg_header_right);
        mTvBack = (TextView)this.findViewById(R.id.reg_header_left);
        
        mTvDeclartion.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        mTvDeclartion.getPaint().setAntiAlias(true);
        
        OnClickListener mclicklistener = new OnClickListener()
        {
			@Override
			public void onClick(View v) 
			{
				switch(v.getId())
				{
					case R.id.cb_reg_agreee:
					{
						
					}break;
					case R.id.tv_reg_declartion:
					{
						Intent intent = new Intent();
				        intent.setClass(RegisterActivity.this, DeclarationActivity.class);
				        startActivity(intent);
					}break;
					case R.id.reg_header_right:
					{
						if(!mCbAgree.isChecked())
						{
							launchToast(R.string.declartion_not_checked_error);
							return;
						}
						mEmail = mEtEmail.getText().toString();
						mEmailConfirm = mEtEmailConfirm.getText().toString();
						mPwd = mEtPwd.getText().toString();
						mPwdConfirm = mEtPwdConfirm.getText().toString();
						mNickname = mEtNickname.getText().toString();
						//email
						if(null == mEmail || mEmail.equals(""))
						{
							mEtEmail.setText("");
							launchToast(R.string.email_empty_error);
							return;
						}
						if(null == mEmailConfirm || mEmailConfirm.equals(""))
						{
							mEtEmailConfirm.setText("");
							launchToast(R.string.email_confirm_empty_error);
							return;
						}
						if(!mEmail.equals(mEmailConfirm))
						{
							launchToast(R.string.email_not_equal_error);
							return;
						}
						//pwd
						if(null == mPwd || mPwd.equals(""))
						{
							mEtPwd.setText("");
							launchToast(R.string.pwd_empty_error);
							return;
						}
						if(null == mPwdConfirm || mPwdConfirm.equals(""))
						{
							mEtPwdConfirm.setText("");
							launchToast(R.string.pwd_confirm_empty_error);
							return;
						}
						if(!mPwd.equals(mPwdConfirm))
						{
							launchToast(R.string.pwd_not_equal_error);
							return;
						}
						//nickname
						if(null == mNickname || mNickname.equals(""))
						{
							mEtNickname.setText("");
							launchToast(R.string.nickname_empty_error);
							return;
						}
						CreateAccountTask task = new CreateAccountTask();
						mLoadingDialog = ProgressDialog.show(RegisterActivity.this, "please wait", "progressing...");
						task.execute("");
					}break;
					case R.id.reg_header_left:
					{
						finish();
					}
					default:{}break;
				}
			}
        };
        this.mCbAgree.setOnClickListener(mclicklistener);
        this.mTvDeclartion.setOnClickListener(mclicklistener);
        this.mTvBack.setOnClickListener(mclicklistener);
        this.mTvDone.setOnClickListener(mclicklistener);
    }
    
    
    private class CreateAccountTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) 
        {
        	account ac = account.getInstanse();
        	Response res = ac.CheckMailAccountDuplication(mEmail);
        	if(res.errno != Response.SUCCESS)
        	{
        		return res.errmsg;
        	}
        	String isDuplication = (String)res.data.get("isDuplicate");
        	if(!isDuplication.equals("0"))
        	{
        		this.publishProgress(R.id.et_reg_email);
        		this.publishProgress(R.id.et_reg_email_confirm);
        		return RegisterActivity.this.getResources().getString(R.string.email_address_duplication_error);
        	}
        	//create account
        	res = ac.CreateAccount(mEmail, mPwd, mNickname);
        	if(res.errno != Response.SUCCESS)
        	{
        		return res.errmsg;
        	}
            return null;
        }
        @Override
        protected void onProgressUpdate(Integer... p)
        {
        	super.onProgressUpdate(p);
        	EditText v = (EditText)RegisterActivity.this.findViewById(p[0]);
        	v.setText("");
        }
        @Override
        protected void onPostExecute(String result) 
        {
        	super.onPostExecute(result);
        	if(null != result)
        	{
        		launchToast(result);
        	}
        	mLoadingDialog.hide();
        	launchToast(R.string.account_create_ok);
        	Intent i = new Intent();
        	i.setClass(RegisterActivity.this, LoginActivity.class);
        	i.putExtra("remind",R.string.login_remind);
        	i.putExtra("header", R.string.account_reg_ok);
        	startActivity(i);
        }
    }
    
}
